home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / editors / mntemacs.zoo / src / dispnew.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-30  |  42.2 KB  |  1,584 lines

  1. /* Newly written part of redisplay code.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /**
  21.  ** (sjk)++ Include time.h for the input-wait loop.
  22.  **/
  23. #if defined(atarist)
  24. #include <time.h>
  25. #endif 
  26.  
  27. #include <signal.h>
  28.  
  29. #include "config.h"
  30. #include <stdio.h>
  31.  
  32. #ifdef HAVE_TIMEVAL
  33. #ifdef HPUX
  34. #include <time.h>
  35. #else
  36. #include <sys/time.h>
  37. #endif
  38. #endif
  39.  
  40. #ifdef HAVE_TERMIO
  41. #include <termio.h>
  42. #ifdef TCOUTQ
  43. #undef TIOCOUTQ
  44. #define TIOCOUTQ TCOUTQ
  45. #include <fcntl.h>
  46. #endif /* TCOUTQ defined */
  47. #else
  48. #ifndef VMS
  49. #include <sys/ioctl.h>
  50. #endif /* not VMS */
  51. #endif /* not HAVE_TERMIO */
  52.  
  53. /* Allow m- file to inhibit use of FIONREAD.  */
  54. #ifdef BROKEN_FIONREAD
  55. #undef FIONREAD
  56. #endif
  57.  
  58. /* We are unable to use interrupts if FIONREAD is not available,
  59.    so flush SIGIO so we won't try. */
  60. #ifndef FIONREAD
  61. #ifdef SIGIO
  62. #undef SIGIO
  63. #endif
  64. #endif
  65.  
  66. #undef NULL
  67.  
  68. #include "termchar.h"
  69. #include "termopts.h"
  70. #include "cm.h"
  71. #include "dispextern.h"
  72. #include "lisp.h"
  73. #include "buffer.h"
  74. #include "window.h"
  75. #include "commands.h"
  76.  
  77. #define max(a, b) ((a) > (b) ? (a) : (b))
  78. #define min(a, b) ((a) < (b) ? (a) : (b))
  79.  
  80. #ifndef PENDING_OUTPUT_COUNT
  81. /* Get number of chars of output now in the buffer of a stdio stream.
  82.    This ought to be built in in stdio, but it isn't.
  83.    Some s- files override this because their stdio internals differ.  */
  84. #ifdef __GNU_LIBRARY__
  85. #define    PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bp - (FILE)->__buf)
  86. #else
  87. #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
  88. #endif
  89. #endif /* No PENDING_OUTPUT_COUNT */
  90.  
  91. /* Nonzero means do not assume anything about current
  92.    contents of actual terminal screen */
  93.  
  94. int screen_garbaged;
  95.  
  96. /* Desired terminal cursor position (to show position of point),
  97.    origin zero */
  98.  
  99. int cursor_hpos, cursor_vpos;
  100.  
  101. /* Nonzero means last display completed and cursor is really at
  102.    cursor_hpos, cursor_vpos.  Zero means it was preempted. */
  103.  
  104. int display_completed;
  105.  
  106. /* Lisp variable visible-bell; enables use of screen-flash
  107.    instead of audible bell.  */
  108.  
  109. int visible_bell;
  110.  
  111. /* Invert the color of the whole screen, at a low level.  */
  112.  
  113. int inverse_video;
  114.  
  115. /* Line speed of the terminal.  */
  116.  
  117. int baud_rate;
  118.  
  119. /* nil or a symbol naming the window system
  120.    under which emacs is running
  121.    ('x is the only current possibility).  */
  122.  
  123. Lisp_Object Vwindow_system;
  124.  
  125. /* Version number of window system, or nil if no window system.  */
  126.  
  127. Lisp_Object Vwindow_system_version;
  128.  
  129. /* Nonzero means reading single-character input with prompt
  130.    so put cursor on minibuffer after the prompt.  */
  131.  
  132. int cursor_in_echo_area;
  133.  
  134. /* Description of actual screen contents.  */
  135.  
  136. struct matrix *current_screen;
  137.  
  138. /* Description of desired screen contents.  */
  139.  
  140. struct matrix *new_screen;
  141.  
  142. /* Buffer sometimes used to hold partial screen contents.  */
  143.  
  144. struct matrix *temp_screen;
  145.  
  146. /* Stdio stream being used for copy of all terminal output.  */
  147.  
  148. FILE *termscript;
  149.  
  150. /* Structure for info on cursor positioning */
  151.  
  152. struct cm Wcm;
  153.  
  154. int in_display;        /* 1 if in redisplay: can't handle SIGWINCH now.  */
  155.  
  156. int delayed_size_change;  /* 1 means SIGWINCH happened when not safe.  */
  157. int delayed_screen_height;  /* Remembered new screen height.  */
  158. int delayed_screen_width;   /* Remembered new screen width.  */
  159.  
  160. /* This buffer records the history of display preemption.  */
  161.  
  162. struct preempt
  163. {
  164.   /* Number of keyboard characters read so far at preempt.  */
  165.   int keyboard_char_count;
  166.   /* Vertical position at which preemption occurred.  */
  167.   int vpos;
  168. };
  169.  
  170. #define N_PREEMPTIONS 50
  171.  
  172. /* Circular buffer recording recent display preemptions.  */
  173. struct preempt preemptions[N_PREEMPTIONS];
  174.  
  175. /* Index of next element in preemptions.  */
  176. int preemption_index;
  177.  
  178. /* Set these variables in the debugger to force a display preemption.  */
  179. int debug_preemption_vpos = -1;
  180. int debug_preemption_char_count = -1;
  181.  
  182. extern int num_input_chars;
  183.  
  184. /* Free and reallocate current_screen and new_screen.  */
  185.  
  186. struct matrix *make_screen_structure ();
  187.  
  188. remake_screen_structures ()
  189. {
  190.   if (current_screen)
  191.     free_screen_structure (current_screen);
  192.   if (new_screen)
  193.     free_screen_structure (new_screen);
  194.   if (temp_screen)
  195.     free_screen_structure (temp_screen);
  196.  
  197.   current_screen = make_screen_structure (0);
  198.   new_screen = make_screen_structure (0);
  199.   temp_screen = make_screen_structure (1);
  200.  
  201.   if (message_buf)
  202.     message_buf = (char *) xrealloc (message_buf, screen_width + 1);
  203.   else
  204.     message_buf = (char *) xmalloc (screen_width + 1);
  205. }
  206.  
  207. struct matrix *
  208. make_screen_structure (empty)
  209.      int empty;
  210. {
  211.   int i;
  212.   struct matrix *new = (struct matrix *) xmalloc (sizeof (struct matrix));
  213.  
  214.   new->height = screen_height;
  215.   new->width = screen_width;
  216.   new->highlight = (char *) xmalloc (screen_height);
  217.   new->enable = (char *) xmalloc (screen_height);
  218.   new->contents = (unsigned char **) xmalloc (screen_height * sizeof (char *));
  219.   new->used = (int *) xmalloc (screen_height * sizeof (int));
  220.   if (empty)
  221.     {
  222.       /* Make the buffer used by decode_mode_spec.  */
  223.       new->total_contents = (unsigned char *) xmalloc (screen_width + 2);
  224.       bzero (new->contents, screen_height * sizeof (char *));
  225.     }
  226.   else
  227.     {
  228.       /* Add 2 to leave extra bytes at beginning and end of each line.  */ 
  229.       new->total_contents = (unsigned char *) xmalloc (screen_height * (screen_width + 2));
  230.       bzero (new->total_contents, screen_height * (screen_width + 2));
  231.       for (i = 0; i < screen_height; i++)
  232.     new->contents[i] = new->total_contents + i * (screen_width + 2) + 1;
  233.     }
  234.   bzero (new->enable, screen_height);
  235.   return new;
  236. }
  237.  
  238. free_screen_structure (matrix)
  239.      struct matrix *matrix;
  240. {
  241.   if (matrix->total_contents)
  242.     free (matrix->total_contents);
  243.   free (matrix->contents);
  244.   free (matrix->highlight);
  245.   free (matrix->enable);
  246.   free (matrix->used);
  247.   free (matrix);
  248. }
  249.  
  250. /* Return the hash code of contents of line VPOS of screen-matrix M.  */
  251.  
  252. int
  253. line_hash_code (m, vpos)
  254.      struct matrix *m;
  255.      int vpos;
  256. {
  257.   register unsigned char *body;
  258.   register int h = 0;
  259.   /* Give all lighlighted lines the same hash code
  260.      so as to encourage scrolling to leave them in place.  */
  261.   if (m->highlight[vpos])
  262.     return -1;
  263.  
  264.   body = m->contents[vpos];
  265.  
  266.   if (must_write_spaces)
  267.     {
  268.       while (1)
  269.     {
  270.       int c = *body++;
  271.       if (c == 0)
  272.         break;
  273.       h = (((h << 4) + (h >> 24)) & 0x0fffffff) + c - ' ';
  274.     }
  275.     }
  276.   else
  277.     {
  278.       while (1)
  279.     {
  280.       int c = *body++;
  281.       if (c == 0)
  282.         break;
  283.       h = (((h << 4) + (h >> 24)) & 0x0fffffff) + c;
  284.     }
  285.     }
  286.   if (h)
  287.     return h;
  288.   return 1;
  289. }
  290.  
  291. /* Return number of characters in line in M at vpos VPOS,
  292.    except don't count leading and trailing spaces
  293.    unless the terminal requires those to be explicitly output.  */
  294.  
  295. int
  296. line_draw_cost (m, vpos)
  297.      struct matrix *m;
  298.      int vpos;
  299. {
  300.   register unsigned char *body;
  301.   register int i;
  302.  
  303.   if (must_write_spaces)
  304.     return m->used[vpos];
  305.  
  306.   body = m->contents[vpos];
  307.   for (i = m->used[vpos]; i > 0 && body[i - 2] == ' '; i--);
  308.  
  309.   i -= count_blanks (body);
  310.   return max (i, 0);
  311. }
  312.  
  313. /* The functions on this page are the interface from xdisp.c to redisplay.
  314.  
  315.  The only other interface into redisplay is through setting
  316.  cursor_hpos and cursor_vpos (in xdisp.c) and setting screen_garbaged. */
  317.  
  318. /* cancel_line eliminates any request to display a line at position `vpos' */
  319.  
  320. cancel_line (vpos)
  321.      int vpos;
  322. {
  323.   new_screen->enable[vpos] = 0;
  324. }
  325.  
  326. clear_screen_records ()
  327. {
  328.   int i;
  329.  
  330.   bzero (current_screen->enable, screen_height);
  331. }
  332.  
  333. /* Get ready to display on line `vpos'
  334.    and set it up for outputting starting at `hpos' within it.
  335.    Return the text string where that line is stored.  */
  336.  
  337. unsigned char *
  338. get_display_line (vpos, hpos)
  339.      int vpos;
  340.      register int hpos;
  341. {
  342.   if (new_screen->enable[vpos] && new_screen->used[vpos] > hpos)
  343.     abort ();
  344.   if (! new_screen->enable[vpos])
  345.     {
  346.       new_screen->used[vpos] = 0;
  347.       new_screen->highlight[vpos] = 0;
  348.       new_screen->enable[vpos] = 1;
  349.     }
  350.  
  351.   if (hpos > new_screen->used[vpos])
  352.     {
  353.       unsigned char *p = new_screen->contents[vpos] + new_screen->used[vpos];
  354.       unsigned char *end = new_screen->contents[vpos] + hpos;
  355.       new_screen->used[vpos] = hpos;
  356.       while (p != end)
  357.     *p++ = ' ';
  358.     }
  359.  
  360.   return new_screen->contents[vpos];
  361. }
  362.  
  363. /* Scroll lines from vpos `from' up to but not including vpos `end'
  364.  down by `amount' lines (`amount' may be negative).
  365.  Returns nonzero if done, zero if terminal cannot scroll them. */
  366.  
  367. int
  368. scroll_screen_lines (from, end, amount)
  369.      int from, end, amount;
  370. {
  371.   register int i;
  372.  
  373.   if (!line_ins_del_ok)
  374.     return 0;
  375.  
  376.   if (amount == 0)
  377.     return 1;
  378.   if (amount > 0)
  379.     {
  380.       set_terminal_window (end + amount);
  381.       if (!scroll_region_ok)
  382.     ins_del_lines (end, -amount);
  383.       ins_del_lines (from, amount);
  384.       set_terminal_window (0);
  385.  
  386.       rotate_vector (current_screen->contents + from,
  387.              sizeof (char *) * (end + amount - from),
  388.              amount * sizeof (char *));
  389.       safe_bcopy (current_screen->used + from,
  390.           current_screen->used + from + amount,
  391.           (end - from) * sizeof current_screen->used[0]);
  392.       safe_bcopy (current_screen->highlight + from,
  393.           current_screen->highlight + from + amount,
  394.           (end - from) * sizeof current_screen->highlight[0]);
  395.       safe_bcopy (current_screen->enable + from,
  396.           current_screen->enable + from + amount,
  397.           (end - from) * sizeof current_screen->enable[0]);
  398.       /* Mark the lines made empty by scrolling as enabled, empty and
  399.      normal video.  */
  400.       bzero (current_screen->used + from,
  401.          amount * sizeof current_screen->used[0]);
  402.       bzero (current_screen->highlight + from,
  403.          amount * sizeof current_screen->highlight[0]);
  404.       for (i = from; i < from + amount; i++)
  405.     {
  406.       current_screen->contents[i][0] = '\0';
  407.       current_screen->enable[i] = 1;
  408.     }
  409.     }
  410.   if (amount < 0)
  411.     {
  412.       set_terminal_window (end);
  413.       ins_del_lines (from + amount, amount);
  414.       if (!scroll_region_ok)
  415.     ins_del_lines (end + amount, -amount);
  416.       set_terminal_window (0);
  417.  
  418.       rotate_vector (current_screen->contents + from + amount,
  419.              sizeof (char *) * (end - from - amount),
  420.              (end - from) * sizeof (char *));
  421.       safe_bcopy (current_screen->used + from,
  422.           current_screen->used + from + amount,
  423.           (end - from) * sizeof current_screen->used[0]);
  424.       safe_bcopy (current_screen->highlight + from,
  425.           current_screen->highlight + from + amount,
  426.           (end - from) * sizeof current_screen->highlight[0]);
  427.       safe_bcopy (current_screen->enable + from,
  428.           current_screen->enable + from + amount,
  429.           (end - from) * sizeof current_screen->enable[0]);
  430.       /* Mark the lines made empty by scrolling as enabled, empty and
  431.      normal video.  */
  432.       bzero (current_screen->used + end + amount,
  433.          - amount * sizeof current_screen->used[0]);
  434.       bzero (current_screen->highlight + end + amount,
  435.          - amount * sizeof current_screen->highlight[0]);
  436.       for (i = end + amount; i < end; i++)
  437.     {
  438.       current_screen->contents[i][0] = '\0';
  439.       current_screen->enable[i] = 1;
  440.     }
  441.     }
  442.   return 1;
  443. }
  444.  
  445. /* Rotate a vector of SIZE bytes, by DISTANCE bytes.
  446.    DISTANCE may be negative.  */
  447.  
  448. rotate_vector (vector, size, distance)
  449.      char *vector;
  450.      int size;
  451.      int distance;
  452. {
  453.   char *temp = (char *) alloca (size);
  454.  
  455.   if (distance < 0)
  456.     distance += size;
  457.  
  458.   bcopy (vector, temp + distance, size - distance);
  459.   bcopy (vector + size - distance, temp, distance);
  460.   bcopy (temp, vector, size);
  461. }
  462.  
  463. /* Like bcopy except never gets confused by overlap.  */
  464.  
  465. safe_bcopy (from, to, size)
  466.      char *from, *to;
  467.      int size;
  468. {
  469.   register char *endf;
  470.   register char *endt;
  471.  
  472.   if (size == 0)
  473.     return;
  474.   if (from > to)
  475.     {
  476.       /* If destination is lower in memory, we can go from the beginning.  */
  477.       endf = from + size;
  478.       while (from != endf)
  479.     *to++ = *from++;
  480.       return;
  481.     }
  482.  
  483.   /* If destination is higher in memory, we can go backwards from the end.  */
  484.   endf = from + size;
  485.   endt = to + size;
  486.  
  487.   do
  488.     *--endt = *--endf;
  489.   while (endf != from);
  490. }
  491.  
  492. /* After updating a window w that isn't the full screen wide,
  493.  copy all the columns that w does not occupy
  494.  from current_screen to new_screen,
  495.  so that update_screen will not change those columns.  */
  496.  
  497. preserve_other_columns (w)
  498.      struct window *w;
  499. {
  500.   register int vpos;
  501.   int start = XFASTINT (w->left);
  502.   int end = XFASTINT (w->left) + XFASTINT (w->width);
  503.   int bot = XFASTINT (w->top) + XFASTINT (w->height);
  504.  
  505.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  506.     {
  507.       if (current_screen->enable[vpos] && new_screen->enable[vpos])
  508.     {
  509.       if (start > 0)
  510.         {
  511.           int len;
  512.  
  513.           bcopy (current_screen->contents[vpos],
  514.              new_screen->contents[vpos], start);
  515.           len = min (start, current_screen->used[vpos]);
  516.           if (new_screen->used[vpos] < len)
  517.         new_screen->used[vpos] = len;
  518.         }
  519.       if (current_screen->used[vpos] > end
  520.           && new_screen->used[vpos] < current_screen->used[vpos])
  521.         {
  522.           while (new_screen->used[vpos] < end)
  523.         new_screen->contents[vpos][new_screen->used[vpos]++] = ' ';
  524.           bcopy (current_screen->contents[vpos] + end,
  525.              new_screen->contents[vpos] + end,
  526.              current_screen->used[vpos] - end);
  527.           new_screen->used[vpos] = current_screen->used[vpos];
  528.         }
  529.     }
  530.     }
  531. }
  532.  
  533. /* On discovering that the redisplay for a window was no good,
  534.  cancel the columns of that window,
  535.  so that when the window is displayed over again
  536.  get_display_line will not complain. */
  537.  
  538. cancel_my_columns (w)
  539.      struct window *w;
  540. {
  541.   register int vpos;
  542.   register int start = XFASTINT (w->left);
  543.   register int bot = XFASTINT (w->top) + XFASTINT (w->height);
  544.  
  545.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  546.     if (new_screen->enable[vpos] && new_screen->used[vpos] >= start)
  547.       new_screen->used[vpos] = start;
  548. }
  549.  
  550. /* These functions try to perform directly and immediately on the screen
  551.    the necessary output for one change in the buffer.
  552.    They may return 0 meaning nothing was done if anything is difficult,
  553.    or 1 meaning the output was performed properly.
  554.    They assume that the screen was up to date before the buffer
  555.    change being displayed.  THey make various other assumptions too;
  556.    see command_loop_1 where these are called.  */
  557.  
  558. int
  559. direct_output_for_insert (c)
  560.      int c;
  561. {
  562. #ifndef COMPILER_REGISTER_BUG
  563.   register
  564. #endif COMPILER_REGISTER_BUG
  565.     struct window *w = XWINDOW (selected_window);
  566. #ifndef COMPILER_REGISTER_BUG
  567.   register
  568. #endif COMPILER_REGISTER_BUG
  569.     int hpos = cursor_hpos;
  570. #ifndef COMPILER_REGISTER_BUG
  571.   register
  572. #endif COMPILER_REGISTER_BUG
  573.     int vpos = cursor_vpos;
  574.  
  575.   /* Give up if about to continue line */
  576.   if (hpos - XFASTINT (w->left) + 1 + 1 >= XFASTINT (w->width)
  577.  
  578.   /* Avoid losing if cursor is in invisible text off left margin */
  579.       || XINT (w->hscroll) && hpos == XFASTINT (w->left)
  580.     
  581.   /* Give up if cursor outside window (in minibuf, probably) */
  582.       || cursor_vpos < XFASTINT (w->top)
  583.       || cursor_vpos >= XFASTINT (w->top) + XFASTINT (w->height)
  584.  
  585.   /* Give up if cursor not really at cursor_hpos, cursor_vpos */
  586.       || !display_completed
  587.  
  588.   /* Give up if w is minibuffer and a message is being displayed there */
  589.       || EQ (selected_window, minibuf_window) && echo_area_contents)
  590.     return 0;
  591.  
  592.   current_screen->contents[vpos][hpos] = c;
  593.   unchanged_modified = MODIFF;
  594.   beg_unchanged = GPT - BEG;
  595.   XFASTINT (w->last_point) = point;
  596.   XFASTINT (w->last_point_x) = cursor_hpos;
  597.   XFASTINT (w->last_modified) = MODIFF;
  598.  
  599.   reassert_line_highlight (0, cursor_vpos);
  600.   output_chars (¤t_screen->contents[vpos][hpos], 1);
  601.   fflush (stdout);
  602.   ++cursor_hpos;
  603.   if (hpos == current_screen->used[vpos])
  604.     {
  605.       current_screen->used[vpos] = hpos + 1;
  606.       current_screen->contents[vpos][hpos + 1] = 0;
  607.     }
  608.   return 1;
  609. }
  610.  
  611. int
  612. direct_output_forward_char (n)
  613.      int n;
  614. {
  615.   register struct window *w = XWINDOW (selected_window);
  616.  
  617.   /* Avoid losing if cursor is in invisible text off left margin */
  618.   if (XINT (w->hscroll) && cursor_hpos == XFASTINT (w->left))
  619.     return 0;
  620.  
  621.   cursor_hpos += n;
  622.   XFASTINT (w->last_point_x) = cursor_hpos;
  623.   XFASTINT (w->last_point) = point;
  624.   move_cursor (cursor_vpos, cursor_hpos);
  625.   fflush (stdout);
  626.   return 1;
  627. }
  628.  
  629. /* Update the actual terminal screen based on the data in new_screen.
  630.    Value is nonzero if redisplay stopped due to pending input.
  631.    FORCE nonzero means do not stop for pending input.  */
  632.  
  633. update_screen (force, inhibit_hairy_id)
  634.      int force;
  635.      int inhibit_hairy_id;
  636. {
  637.   register struct display_line **p;
  638.   register struct display_line *l, *lnew;
  639.   register int i;
  640.   int pause;
  641.   int preempt_count = baud_rate / 2400 + 1;
  642.   extern input_pending;
  643.  
  644.   if (screen_height == 0) abort (); /* Some bug zeros some core */
  645.  
  646.   detect_input_pending ();
  647.   if (!force
  648.       && ((num_input_chars == debug_preemption_char_count
  649.        && debug_preemption_vpos == screen_height - 1)
  650.       || input_pending))
  651.     {
  652.       pause = screen_height;
  653.       goto do_pause;
  654.     }
  655.  
  656.   update_begin ();
  657.  
  658.   if (!line_ins_del_ok)
  659.     inhibit_hairy_id = 1;
  660.  
  661.   /* Don't compute for i/d line if just want cursor motion. */
  662.   for (i = 0; i < screen_height; i++)
  663.     if (new_screen->enable)
  664.       break;
  665.  
  666.   /* Try doing i/d line, if not yet inhibited.  */
  667.   if (!inhibit_hairy_id && i < screen_height)
  668.     force |= scrolling ();
  669.  
  670.   /* Update the individual lines as needed.  Do bottom line first.  */
  671.  
  672.   if (new_screen->enable[screen_height - 1])
  673.     update_line (screen_height - 1);
  674.   for (i = 0; i < screen_height - 1 && (force || !input_pending); i++)
  675.     {
  676.       if (!force && num_input_chars == debug_preemption_char_count
  677.       && debug_preemption_vpos == i)
  678.     break;
  679.       if (new_screen->enable[i])
  680.     {
  681.       /* Flush out every so many lines.
  682.          Also flush out if likely to have more than 1k buffered
  683.          otherwise.   I'm told that telnet connections get really
  684.          screwed by more than 1k output at once.  */
  685.       int outq = PENDING_OUTPUT_COUNT (stdout);
  686.       if (outq > 900
  687.           || (outq > 20 && ((i - 1) % preempt_count == 0)))
  688.         {
  689.           fflush (stdout);
  690.           if (preempt_count == 1)
  691.         {
  692. #ifdef TIOCOUTQ
  693.           if (ioctl (0, TIOCOUTQ, &outq) < 0)
  694.             /* Probably not a tty.  Ignore the error and reset
  695.              * the outq count. */
  696.             outq = PENDING_OUTPUT_COUNT (stdout);
  697. #endif
  698.           outq *= 10;
  699.           sleep (outq / baud_rate);
  700.         }
  701.         }
  702.       if ((i - 1) % preempt_count == 0)
  703.         detect_input_pending ();
  704.       /* Now update this line.  */
  705.       update_line (i);
  706.     }
  707.     }
  708.   pause = (i < screen_height - 1) ? i + 1 : 0;
  709.  
  710.   /* Now just clean up termcap drivers and set cursor, etc.  */
  711.   if (!pause)
  712.     {
  713.       if (cursor_in_echo_area < 0)
  714.     move_cursor (screen_height - 1, 0);
  715.       else if (cursor_in_echo_area > 0
  716.            && !current_screen->enable[screen_height - 1])
  717.     move_cursor (screen_height - 1, 0);
  718.       else if (cursor_in_echo_area)
  719.     move_cursor (screen_height - 1,
  720.              min (screen_width - 1,
  721.               current_screen->used[screen_height - 1]));
  722.       else
  723.     move_cursor (cursor_vpos, max (min (cursor_hpos, screen_width - 1), 0));
  724.     }
  725.  
  726.   update_end ();
  727.  
  728.   if (termscript)
  729.     fflush (termscript);
  730.   fflush (stdout);
  731.  
  732.   /* Here if output is preempted because input is detected.  */
  733.  do_pause:
  734.  
  735.   if (screen_height == 0) abort (); /* Some bug zeros some core */
  736.   display_completed = !pause;
  737.   if (pause)
  738.     {
  739.       preemptions[preemption_index].vpos = pause - 1;
  740.       preemptions[preemption_index].keyboard_char_count = num_input_chars;
  741.       preemption_index++;
  742.       if (preemption_index == N_PREEMPTIONS)
  743.     preemption_index = 0;
  744.     }
  745.  
  746.   bzero (new_screen->enable, screen_height);
  747.   return pause;
  748. }
  749.  
  750. /* Called when about to quit, to check for doing so
  751.    at an improper time.  */
  752.  
  753. void
  754. quit_error_check ()
  755. {
  756.   if (new_screen == 0)
  757.     return;
  758.   if (new_screen->enable[0])
  759.     abort ();
  760.   if (new_screen->enable[screen_height - 1])
  761.     abort ();
  762. }
  763.  
  764. /* Decide what insert/delete line to do, and do it */
  765.  
  766. scrolling ()
  767. {
  768.   int unchanged_at_top, unchanged_at_bottom;
  769.   int window_size;
  770.   int changed_lines;
  771.   int *old_hash = (int *) alloca (screen_height * sizeof (int));
  772.   int *new_hash = (int *) alloca (screen_height * sizeof (int));
  773.   int *draw_cost = (int *) alloca (screen_height * sizeof (int));
  774.   register int i;
  775.   int free_at_end_vpos = screen_height;
  776.   
  777.   /* Compute hash codes of all the lines.
  778.      Also calculate number of changed lines,
  779.      number of unchanged lines at the beginning,
  780.      and number of unchanged lines at the end.  */
  781.  
  782.   changed_lines = 0;
  783.   unchanged_at_top = 0;
  784.   unchanged_at_bottom = screen_height;
  785.   for (i = 0; i < screen_height; i++)
  786.     {
  787.       /* Give up on this scrolling if some old lines are not enabled.  */
  788.       if (!current_screen->enable[i])
  789.     return 0;
  790.       old_hash[i] = line_hash_code (current_screen, i);
  791.       if (!new_screen->enable[i])
  792.     new_hash[i] = old_hash[i];
  793.       else
  794.     new_hash[i] = line_hash_code (new_screen, i);
  795.       if (old_hash[i] != new_hash[i])
  796.     {
  797.       changed_lines++;
  798.       unchanged_at_bottom = screen_height - i - 1;
  799.     }
  800.       else if (i == unchanged_at_top)
  801.     unchanged_at_top++;
  802.       /* If line is not changing, its redraw cost is infinite,
  803.      since we can't redraw it.  */
  804.       if (!new_screen->enable[i])
  805.     draw_cost[i] = INFINITY;
  806.       else
  807.     draw_cost[i] = line_draw_cost (new_screen, i);
  808.     }
  809.  
  810.   /* If changed lines are few, don't allow preemption, don't scroll.  */
  811.   if (changed_lines < baud_rate / 2400 || unchanged_at_bottom == screen_height)
  812.     return 1;
  813.  
  814.   window_size = screen_height - unchanged_at_top - unchanged_at_bottom;
  815.  
  816.   if (scroll_region_ok)
  817.     free_at_end_vpos -= unchanged_at_bottom;
  818.   else if (memory_below_screen)
  819.     free_at_end_vpos = -1;
  820.  
  821.   /* If large window, fast terminal and few lines in common between
  822.      current_screen and new_screen, don't bother with i/d calc.  */
  823.   if (window_size >= 18 && baud_rate > 2400
  824.       && (window_size >=
  825.       10 * scrolling_max_lines_saved (unchanged_at_top,
  826.                       screen_height - unchanged_at_bottom,
  827.                       old_hash, new_hash, draw_cost)))
  828.     return 0;
  829.  
  830.   scrolling_1 (window_size, unchanged_at_top, unchanged_at_bottom,
  831.            draw_cost + unchanged_at_top - 1,
  832.            old_hash + unchanged_at_top - 1,
  833.            new_hash + unchanged_at_top - 1,
  834.            free_at_end_vpos - unchanged_at_top);
  835.  
  836.   return 0;
  837. }
  838.  
  839. update_line (vpos)
  840.      int vpos;
  841. {
  842.   register unsigned char *obody, *nbody, *op1, *op2, *np1;
  843.   int tem;
  844.   int osp, nsp, begmatch, endmatch, olen, nlen;
  845.   int save;
  846.   unsigned char *temp;
  847.  
  848.   /* Check for highlighting change.  */
  849.   if (new_screen->highlight[vpos]
  850.       != (current_screen->enable[vpos] && current_screen->highlight[vpos]))
  851.     {
  852.       change_line_highlight (new_screen->highlight[vpos], vpos,
  853.                  (current_screen->enable[vpos]
  854.                   ? current_screen->used[vpos] : 0));
  855.       current_screen->enable[vpos] = 0;
  856.     }
  857.   else
  858.     reassert_line_highlight (new_screen->highlight[vpos], vpos);
  859.  
  860.   /* ??? */
  861.   if (! current_screen->enable[vpos])
  862.     {
  863.       olen = 0;
  864.     }
  865.   else
  866.     {
  867.       obody = current_screen->contents[vpos];
  868.       olen = current_screen->used[vpos];
  869.       if (! current_screen->highlight[vpos])
  870.     {
  871.       /* Note obody[-1] is always 0.  */
  872.       if (!must_write_spaces)
  873.         while (obody[olen - 1] == ' ')
  874.           olen--;
  875.     }
  876.       else
  877.     {
  878.       /* For an inverse-video line, remember we gave it
  879.          spaces all the way to the screen edge
  880.          so that the reverse video extends all the way across.  */
  881.       while (olen < screen_width - 1)
  882.         obody[olen++] = ' ';
  883.     }
  884.     }
  885.  
  886.   /* One way or another, this will enable the line being updated.  */
  887.   current_screen->enable[vpos] = 1;
  888.   current_screen->used[vpos] = new_screen->used[vpos];
  889.   current_screen->highlight[vpos] = new_screen->highlight[vpos];
  890.  
  891.   if (!new_screen->enable[vpos])
  892.     {
  893.       nlen = 0;
  894.       goto just_erase;
  895.     }
  896.  
  897.   nbody = new_screen->contents[vpos];
  898.   nlen = new_screen->used[vpos];
  899.  
  900.   /* Pretend trailing spaces are not there at all,
  901.      unless for one reason or another we must write all spaces.  */
  902.   /* We know that the previous character byte contains 0.  */
  903.   if (! new_screen->highlight[vpos])
  904.     {
  905.       if (!must_write_spaces)
  906.     while (nbody[nlen - 1] == ' ')
  907.       nlen--;
  908.     }
  909.   else
  910.     {
  911.       /* For an inverse-video line, give it extra trailing spaces
  912.      all the way to the screen edge
  913.      so that the reverse video extends all the way across.  */
  914.       while (nlen < screen_width - 1)
  915.     nbody[nlen++] = ' ';
  916.     }
  917.  
  918.   /* If there's no i/d char, quickly do the best we can without it.  */
  919.   if (!char_ins_del_ok)
  920.     {
  921.       int i,j;
  922.  
  923.       for (i = 0; i < nlen; i++)
  924.     {
  925.       if (i >= olen || nbody[i] != obody[i])
  926.         {
  927.           /* We found a non-matching char.  */
  928.           move_cursor (vpos, i);
  929.           for (j = 1; (i + j < nlen &&
  930.                (i + j >= olen || nbody[i+j] != obody[i+j]));
  931.            j++);
  932.           /* Output this run of non-matching chars.  */ 
  933.           output_chars (nbody + i, j);
  934.           i += j - 1;
  935.           /* Now find the next non-match.  */
  936.         }
  937.     }
  938.       /* Clear the rest of the line, or the non-clear part of it.  */
  939.       if (olen > nlen)
  940.     {
  941.       move_cursor (vpos, nlen);
  942.       clear_end_of_line (olen);
  943.     }
  944.  
  945.       /* Exchange contents between current_screen and new_screen.  */
  946.       temp = new_screen->contents[vpos];
  947.       new_screen->contents[vpos] = current_screen->contents[vpos];
  948.       current_screen->contents[vpos] = temp;
  949.       return;
  950.     }
  951.  
  952.   if (!olen)
  953.     {
  954.       nsp = (must_write_spaces || new_screen->highlight[vpos])
  955.           ? 0 : count_blanks (nbody);
  956.       if (nlen > nsp)
  957.     {
  958.       move_cursor (vpos, nsp);
  959.       output_chars (nbody + nsp, nlen - nsp);
  960.     }
  961.  
  962.       /* Exchange contents between current_screen and new_screen.  */
  963.       temp = new_screen->contents[vpos];
  964.       new_screen->contents[vpos] = current_screen->contents[vpos];
  965.       current_screen->contents[vpos] = temp;
  966.       return;
  967.     }
  968.  
  969.   obody[olen] = 1;
  970.   save = nbody[nlen];
  971.   nbody[nlen] = 0;
  972.  
  973.   /* Compute number of leading blanks in old and new contents.  */
  974.   osp = count_blanks (obody);
  975.   if (!new_screen->highlight[vpos])
  976.     nsp = count_blanks (nbody);
  977.   else
  978.     nsp = 0;
  979.  
  980.   /* Compute number of matching chars starting with first nonblank.  */
  981.   begmatch = count_match (obody + osp, nbody + nsp);
  982.  
  983.   /* Spaces in new match implicit space past the end of old.  */
  984.   /* A bug causing this to be a no-op was fixed in 18.29.  */
  985.   if (!must_write_spaces && osp + begmatch == olen)
  986.     {
  987.       np1 = nbody + nsp;
  988.       while (np1[begmatch] == ' ')
  989.     begmatch++;
  990.     }
  991.  
  992.   /* Avoid doing insert/delete char
  993.      just cause number of leading spaces differs
  994.      when the following text does not match. */
  995.   if (begmatch == 0 && osp != nsp)
  996.     osp = nsp = min (osp, nsp);
  997.  
  998.   /* Find matching characters at end of line */
  999.   op1 = obody + olen;
  1000.   np1 = nbody + nlen;
  1001.   op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
  1002.   while (op1 > op2 && op1[-1] == np1[-1])
  1003.     {
  1004.       op1--;
  1005.       np1--;
  1006.     }
  1007.   endmatch = obody + olen - op1;
  1008.  
  1009.   /* Put correct value back in nbody[nlen].
  1010.      This is important because direct_output_for_insert
  1011.      can write into the line at a later point.  */
  1012.   nbody[nlen] = save;
  1013.  
  1014.   /* tem gets the distance to insert or delete.
  1015.      endmatch is how many characters we save by doing so.
  1016.      Is it worth it?  */
  1017.  
  1018.   tem = (nlen - nsp) - (olen - osp);
  1019.   if (endmatch && tem && endmatch <= DCICcost[tem])
  1020.     endmatch = 0;
  1021.  
  1022.   /* nsp - osp is the distance to insert or delete.
  1023.      begmatch + endmatch is how much we save by doing so.
  1024.      Is it worth it?  */
  1025.  
  1026.   if (begmatch + endmatch > 0 && nsp != osp
  1027.       && begmatch + endmatch <= DCICcost[nsp - osp])
  1028.     {
  1029.       begmatch = 0;
  1030.       endmatch = 0;
  1031.       osp = nsp = min (osp, nsp);
  1032.     }
  1033.  
  1034.   /* Now go through the line, inserting, writing and deleting as appropriate.  */
  1035.  
  1036.   if (osp > nsp)
  1037.     {
  1038.       move_cursor (vpos, nsp);
  1039.       delete_chars (osp - nsp);
  1040.     }
  1041.   else if (nsp > osp)
  1042.     {
  1043.       /* If going to delete chars later in line
  1044.      and insert earlier in the line,
  1045.      must delete first to avoid losing data in the insert */
  1046.       if (endmatch && nlen < olen + nsp - osp)
  1047.     {
  1048.       move_cursor (vpos, nlen - endmatch + osp - nsp);
  1049.       delete_chars (olen + nsp - osp - nlen);
  1050.       olen = nlen - (nsp - osp);
  1051.     }
  1052.       move_cursor (vpos, osp);
  1053.       insert_chars ((char *)0, nsp - osp);
  1054.     }
  1055.   olen += nsp - osp;
  1056.  
  1057.   tem = nsp + begmatch + endmatch;
  1058.   if (nlen != tem || olen != tem)
  1059.     {
  1060.       move_cursor (vpos, nsp + begmatch);
  1061.       if (!endmatch || nlen == olen)
  1062.     {
  1063.       /* If new text being written reaches right margin,
  1064.          there is no need to do clear-to-eol at the end.
  1065.          (and it would not be safe, since cursor is not
  1066.          going to be "at the margin" after the text is done) */
  1067.       if (nlen == screen_width)
  1068.         olen = 0;
  1069.       output_chars (nbody + nsp + begmatch, nlen - tem);
  1070. #ifdef obsolete
  1071. /* the following code loses disastrously if tem == nlen.
  1072.    Rather than trying to fix that case, I am trying the simpler
  1073.    solution found above.  */
  1074.       /* If the text reaches to the right margin,
  1075.          it will lose one way or another (depending on AutoWrap)
  1076.          to clear to end of line after outputting all the text.
  1077.          So pause with one character to go and clear the line then.  */
  1078.       if (nlen == screen_width && fast_clear_end_of_line && olen > nlen)
  1079.         {
  1080.           /* endmatch must be zero, and tem must equal nsp + begmatch */
  1081.           output_chars (nbody + tem, nlen - tem - 1);
  1082.           clear_end_of_line (olen);
  1083.           olen = 0;        /* Don't let it be cleared again later */
  1084.           output_chars (nbody + nlen - 1, 1);
  1085.         }
  1086.       else
  1087.         output_chars (nbody + nsp + begmatch, nlen - tem);
  1088. #endif
  1089.     }
  1090.       else if (nlen > olen)
  1091.     {
  1092.       output_chars (nbody + nsp + begmatch, olen - tem);
  1093.       insert_chars (nbody + nsp + begmatch + olen - tem, nlen - olen);
  1094.       olen = nlen;
  1095.     }
  1096.       else if (olen > nlen)
  1097.     {
  1098.       output_chars (nbody + nsp + begmatch, nlen - tem);
  1099.       delete_chars (olen - nlen);
  1100.       olen = nlen;
  1101.     }
  1102.     }
  1103.  
  1104.  just_erase:
  1105.   /* If any unerased characters remain after the new line, erase them.  */
  1106.   if (olen > nlen)
  1107.     {
  1108.       move_cursor (vpos, nlen);
  1109.       clear_end_of_line (olen);
  1110.     }
  1111.   
  1112.   /* Exchange contents between current_screen and new_screen.  */
  1113.   temp = new_screen->contents[vpos];
  1114.   new_screen->contents[vpos] = current_screen->contents[vpos];
  1115.   current_screen->contents[vpos] = temp;
  1116. }
  1117.  
  1118. count_blanks (str)
  1119.      char *str;
  1120. {
  1121.   register char *p = str;
  1122.   while (*str++ == ' ');
  1123.   return str - p - 1;
  1124. }
  1125.  
  1126. count_match (str1, str2)
  1127.      char *str1, *str2;
  1128. {
  1129.   register char *p1 = str1;
  1130.   register char *p2 = str2;
  1131.   while (*p1++ == *p2++);
  1132.   return p1 - str1 - 1;
  1133. }
  1134.  
  1135. DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
  1136.   1, 1, "FOpen termscript file: ",
  1137.   "Start writing all terminal output to FILE as well as the terminal.\n\
  1138. FILE = nil means just close any termscript file currently open.")
  1139.   (file)
  1140.      Lisp_Object file;
  1141. {
  1142.   if (termscript != 0) fclose (termscript);
  1143.   termscript = 0;
  1144.  
  1145.   if (! NULL (file))
  1146.     {
  1147.       file = Fexpand_file_name (file, Qnil);
  1148.       termscript = fopen ((char *)XSTRING (file)->data, "w");
  1149.       if (termscript == 0)
  1150.     report_file_error ("Opening termscript", Fcons (file, Qnil));
  1151.     }
  1152.   return Qnil;
  1153. }
  1154.  
  1155. DEFUN ("set-screen-height", Fset_screen_height, Sset_screen_height, 1, 2, 0,
  1156.   "Tell redisplay that the screen has LINES lines.\n\
  1157. Optional second arg non-nil means that redisplay should use LINES lines\n\
  1158. but that the idea of the actual height of the screen should not be changed.")
  1159.   (n, pretend)
  1160.      Lisp_Object n, pretend;
  1161. {
  1162.   CHECK_NUMBER (n, 0);
  1163.   change_screen_size (XINT (n), 0, !NULL (pretend));
  1164.   return Qnil;
  1165. }
  1166.  
  1167. DEFUN ("set-screen-width", Fset_screen_width, Sset_screen_width, 1, 2, 0,
  1168.   "Tell redisplay that the screen has COLS columns.\n\
  1169. Optional second arg non-nil means that redisplay should use COLS columns\n\
  1170. but that the idea of the actual width of the screen should not be changed.")
  1171.   (n, pretend)
  1172.      Lisp_Object n, pretend;
  1173. {
  1174.   CHECK_NUMBER (n, 0);
  1175.   change_screen_size (0, XINT (n), !NULL (pretend));
  1176.   return Qnil;
  1177. }
  1178.  
  1179. DEFUN ("screen-height", Fscreen_height, Sscreen_height, 0, 0, 0,
  1180.   "Return number of lines on screen available for display.")
  1181.   ()
  1182. {
  1183.   return make_number (screen_height);
  1184. }
  1185.  
  1186. DEFUN ("screen-width", Fscreen_width, Sscreen_width, 0, 0, 0,
  1187.   "Return number of columns on screen available for display.")
  1188.   ()
  1189. {
  1190.   return make_number (screen_width);
  1191. }
  1192.  
  1193. #ifdef SIGWINCH
  1194. window_change_signal ()
  1195. {
  1196.   int width, height;
  1197.   extern int errno;
  1198.   int old_errno = errno;
  1199.  
  1200.   get_screen_size (&width, &height);
  1201.   /* Record the new size, but don't reallocate the data structures now.
  1202.      Let that be done later outside of the signal handler.  */
  1203.   in_display++;
  1204.   change_screen_size (height, width, 0);
  1205.   in_display--;
  1206.   signal (SIGWINCH, (__Sigfunc)window_change_signal);
  1207.  
  1208.   errno = old_errno;
  1209. }
  1210. #endif /* SIGWINCH */
  1211.  
  1212. /* Do any change in screen size that was requested by a signal.  */
  1213.  
  1214. do_pending_window_change ()
  1215. {
  1216.   /* If change_screen_size should have run before, run it now.  */
  1217.   while (delayed_size_change)
  1218.     {
  1219.       int newwidth = delayed_screen_width;
  1220.       int newheight = delayed_screen_height;
  1221.       delayed_size_change = 0;
  1222.       change_screen_size_1 (newheight, newwidth, 0);
  1223.     }
  1224. }
  1225.  
  1226. /* Change the screen height and/or width.  Values may be given as zero to
  1227.    indicate no change is to take place.
  1228.    PRETEND is normally 0; 1 means change used-size only
  1229.    but don't change the size used for calculations;
  1230.    -1 means don't redisplay.  */
  1231.  
  1232. change_screen_size (newlength, newwidth, pretend)
  1233.      register int newlength, newwidth, pretend;
  1234. {
  1235.   /* If we can't deal with the change now, queue it for later.  */
  1236.   if (in_display)
  1237.     {
  1238.       delayed_screen_width = newwidth;
  1239.       delayed_screen_height = newlength;
  1240.       delayed_size_change = 1;
  1241.       return;
  1242.     }
  1243.   delayed_size_change = 0;
  1244.   change_screen_size_1 (newlength, newwidth, pretend);
  1245. }
  1246.  
  1247. change_screen_size_1 (newlength, newwidth, pretend)
  1248.      register int newlength, newwidth, pretend;
  1249. {
  1250.   if ((newlength == 0 || newlength == screen_height)
  1251.       && (newwidth == 0 || newwidth == screen_width))
  1252.     return;
  1253.   if (newlength && newlength != screen_height)
  1254.     {
  1255.       set_window_height (XWINDOW (minibuf_window)->prev, newlength - 1, 0);
  1256.       XFASTINT (XWINDOW (minibuf_window)->top) = newlength - 1;
  1257.       set_window_height (minibuf_window, 1, 0);
  1258.       screen_height = newlength;
  1259.       if (pretend <= 0)
  1260.     ScreenRows = newlength;
  1261.       set_terminal_window (0);
  1262.     }
  1263.   if (newwidth && newwidth != screen_width)
  1264.     {
  1265.       set_window_width (XWINDOW (minibuf_window)->prev, newwidth, 0);
  1266.       set_window_width (minibuf_window, newwidth, 0);
  1267.       screen_width = newwidth;
  1268.       if (pretend <= 0)
  1269.     ScreenCols = newwidth;
  1270.     }
  1271.   remake_screen_structures ();
  1272.   screen_garbaged = 1;
  1273.   calculate_costs ();
  1274.   if (pretend >= 0)
  1275.     redisplay_preserve_echo_area ();
  1276. }
  1277.  
  1278. DEFUN ("baud-rate", Fbaud_rate, Sbaud_rate, 0, 0, 0,
  1279.   "Return the output baud rate of the terminal.")
  1280.   ()
  1281. {
  1282.   Lisp_Object temp;
  1283.   XSET (temp, Lisp_Int, baud_rate);
  1284.   return temp;
  1285. }
  1286.  
  1287. DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
  1288.   Ssend_string_to_terminal, 1, 1, 0,
  1289.   "Send STRING to the terminal without alteration.\n\
  1290. Control characters in STRING will have terminal-dependent effects.")
  1291.   (str)
  1292.      Lisp_Object str;
  1293. {
  1294.   CHECK_STRING (str, 0);
  1295.   fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, stdout);
  1296.   fflush (stdout);
  1297.   if (termscript)
  1298.     {
  1299.       fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, termscript);
  1300.       fflush (termscript);
  1301.     }
  1302.   return Qnil;
  1303. }
  1304.  
  1305. DEFUN ("ding", Fding, Sding, 0, 1, 0,
  1306.   "Beep, or flash the screen.\n\
  1307. Terminates any keyboard macro currently executing unless an argument\n\
  1308. is given.")
  1309.   (arg)
  1310.   Lisp_Object arg;
  1311. {
  1312.   if (!NULL (arg))
  1313.     {
  1314.       bell ();
  1315.       fflush (stdout);
  1316.     }
  1317.   else
  1318.     bell ();
  1319.   return Qnil;
  1320. }
  1321.  
  1322. bell ()
  1323. {
  1324.   if (noninteractive)
  1325.     putchar (07);
  1326.   else if (!FROM_KBD)  /* Stop executing a keyboard macro. */
  1327.     error ("Keyboard macro terminated by a command ringing the bell");
  1328.   else
  1329.     ring_bell ();
  1330.   fflush (stdout);
  1331. }
  1332.  
  1333. DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 1, 0,
  1334.   "Pause, without updating display, for ARG seconds.")
  1335.   (n)
  1336.      Lisp_Object n;
  1337. {
  1338.   register int t;
  1339. #ifndef subprocesses
  1340. #ifdef HAVE_TIMEVAL
  1341.   struct timeval timeout, end_time, garbage1;
  1342. #endif /* HAVE_TIMEVAL */
  1343. #endif /* no subprocesses */
  1344.  
  1345.   CHECK_NUMBER (n, 0);
  1346.   t = XINT (n);
  1347.   if (t <= 0)
  1348.     return Qnil;
  1349.  
  1350. #ifdef subprocesses
  1351.   wait_reading_process_input (t, 0, 0);
  1352. #else /* No subprocesses */
  1353.   immediate_quit = 1;
  1354.   QUIT;
  1355.  
  1356. #ifdef VMS
  1357.   sys_sleep (t);
  1358. #else /* not VMS */
  1359. /* The reason this is done this way 
  1360.     (rather than defined (H_S) && defined (H_T))
  1361.    is because the VMS preprocessor doesn't grok `defined' */
  1362. #ifdef HAVE_SELECT
  1363. #ifdef HAVE_TIMEVAL
  1364.   gettimeofday (&end_time, &garbage1);
  1365.   end_time.tv_sec += t;
  1366.  
  1367.   while (1)
  1368.     {
  1369.       gettimeofday (&timeout, &garbage1);
  1370.       timeout.tv_sec = end_time.tv_sec - timeout.tv_sec;
  1371.       timeout.tv_usec = end_time.tv_usec - timeout.tv_usec;
  1372.       if (timeout.tv_usec < 0)
  1373.     timeout.tv_usec += 1000000,
  1374.       timeout.tv_sec--;
  1375.       if (timeout.tv_sec < 0)
  1376.     break;
  1377.       if (!select (1, 0, 0, 0, &timeout))
  1378.     break;
  1379.     }
  1380. #else /* not HAVE_TIMEVAL */
  1381.   /* Is it safe to quit out of `sleep'?  I'm afraid to trust it.  */
  1382.   sleep (t);
  1383. #endif /* HAVE_TIMEVAL */
  1384. #else /* not HAVE_SELECT */
  1385.   sleep (t);
  1386. #endif /* HAVE_SELECT */
  1387. #endif /* not VMS */
  1388.   
  1389.   immediate_quit = 0;
  1390. #endif /* no subprocesses */
  1391.   return Qnil;
  1392. }
  1393.  
  1394. DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 2, 0,
  1395.   "Perform redisplay, then wait for ARG seconds or until input is available.\n\
  1396. Optional second arg non-nil means don't redisplay.\n\
  1397. Redisplay is preempted as always if input arrives, and does not happen\n\
  1398. if input is available before it starts.\n\
  1399. Value is t if waited the full time with no input arriving.")
  1400.   (n, nodisp)
  1401.      Lisp_Object n, nodisp;
  1402. {
  1403. #ifndef subprocesses
  1404. #ifdef HAVE_TIMEVAL
  1405.   struct timeval timeout;
  1406. #else
  1407.   int timeout_sec;
  1408. #endif
  1409.   int waitchannels;
  1410. #endif /* no subprocesses */
  1411.  
  1412.   CHECK_NUMBER (n, 0);
  1413.  
  1414.   if (detect_input_pending ())
  1415.     return Qnil;
  1416.  
  1417. if (EQ (nodisp, Qnil))
  1418.     redisplay_preserve_echo_area ();
  1419. /**
  1420.  ** (sjk)++ check for input, quite after start seconds.
  1421.  **/
  1422. #if defined(atarist) && !defined(__MINT__)
  1423. if (XINT(n) != 0)
  1424.   { unsigned int start; 
  1425.     start = clock();
  1426.     while (clock() - start < XINT(n) * CLK_TCK)
  1427.       if (console_input_status(fileno(stdin)) != 0) return(Qt);
  1428.     return(Qnil);
  1429.   }
  1430.  else return(Qnil);
  1431. #else 
  1432.  
  1433.   if (XINT (n) > 0)
  1434.     {
  1435. #ifdef subprocesses
  1436. #ifdef SIGIO
  1437.       gobble_input ();
  1438. #endif                /* SIGIO */
  1439.       wait_reading_process_input (XINT (n), 1, 1);
  1440. #else                /* no subprocesses */
  1441.       immediate_quit = 1;
  1442.       QUIT;
  1443.  
  1444.       waitchannels = 1;
  1445. #ifdef VMS
  1446.       input_wait_timeout (XINT (n));
  1447. #else                /* not VMS */
  1448. #ifndef HAVE_TIMEVAL
  1449.       timeout_sec = XINT (n);
  1450.       select (1, &waitchannels, 0, 0, &timeout_sec);
  1451. #else                /* HAVE_TIMEVAL */
  1452.       timeout.tv_sec = XINT (n);  
  1453.       timeout.tv_usec = 0;
  1454.       select (1, &waitchannels, 0, 0, &timeout);
  1455. #endif                /* HAVE_TIMEVAL */
  1456. #endif                /* not VMS */
  1457.  
  1458.       immediate_quit = 0;
  1459. #endif                /* no subprocesses */
  1460.     }
  1461.   return detect_input_pending () ? Qnil : Qt;
  1462. #endif /* ! atarist */ 
  1463. }
  1464.  
  1465. char *terminal_type;
  1466.  
  1467. /* Initialization done when Emacs fork is started, before doing stty. */
  1468. /* Determine terminal type and set terminal_driver */
  1469. /* Then invoke its decoding routine to set up variables
  1470.   in the terminal package */
  1471.  
  1472. init_display ()
  1473. {
  1474. #ifdef HAVE_X_WINDOWS
  1475.   extern Lisp_Object Vxterm;
  1476.   Vxterm = Qnil;
  1477. #endif
  1478.  
  1479.   Vwindow_system = Qnil;
  1480. /** 
  1481.  ** (sjk)++ We do have an alternate key...
  1482.  **/
  1483. #if !defined(atarist)
  1484.   meta_key = 0;
  1485. #else 
  1486.   meta_key = 1;
  1487. #endif
  1488.  
  1489.   inverse_video = 0;
  1490.   cursor_in_echo_area = 0;
  1491.   terminal_type = (char *) 0;
  1492.  
  1493.   if (!inhibit_window_system)
  1494.     {
  1495. #ifdef HAVE_X_WINDOWS
  1496.       extern char *alternate_display;
  1497.       char *disp = (char *) egetenv ("DISPLAY");
  1498.  
  1499.       /* Note KSH likes to provide an empty string as an envvar value.  */
  1500.       if (alternate_display || (disp && *disp))
  1501.     {
  1502.       x_term_init ();
  1503.       Vxterm = Qt;
  1504.       Vwindow_system = intern ("x");
  1505. #ifdef X11
  1506.       Vwindow_system_version = make_number (11);
  1507. #else
  1508.       Vwindow_system_version = make_number (10);
  1509. #endif
  1510.       goto term_init_done;
  1511.     }
  1512. #endif /* HAVE_X_WINDOWS */
  1513.       ;
  1514.     }
  1515.   /* Record we aren't using a window system.  */
  1516.   inhibit_window_system = 1;
  1517.  
  1518.   /* Look at the TERM variable */
  1519.   terminal_type = (char *) getenv ("TERM");
  1520.   if (!terminal_type)
  1521.     {
  1522. #ifdef VMS
  1523.       fprintf (stderr, "Please specify your terminal type.\n\
  1524. For types defined in VMS, use  set term /device=TYPE.\n\
  1525. For types not defined in VMS, use  define emacs_term \"TYPE\".\n\
  1526. \(The quotation marks are necessary since terminal types are lower case.)\n");
  1527. #else
  1528.       fprintf (stderr, "Please set the environment variable TERM; see tset(1).\n");
  1529. #endif
  1530.       exit (1);
  1531.     }
  1532.   term_init (terminal_type);
  1533.  
  1534.  term_init_done:
  1535.   remake_screen_structures ();
  1536.   calculate_costs ();
  1537.  
  1538. #ifdef SIGWINCH
  1539. #ifndef CANNOT_DUMP
  1540.   if (initialized)
  1541. #endif /* CANNOT_DUMP */
  1542.     if (inhibit_window_system)
  1543.       signal (SIGWINCH, (__Sigfunc)window_change_signal);
  1544. #endif /* SIGWINCH */
  1545. }
  1546.  
  1547. syms_of_display ()
  1548. {
  1549.   defsubr (&Sopen_termscript);
  1550.   defsubr (&Sding);
  1551.   defsubr (&Ssit_for);
  1552.   defsubr (&Sscreen_height);
  1553.   defsubr (&Sscreen_width);
  1554.   defsubr (&Sset_screen_height);
  1555.   defsubr (&Sset_screen_width);
  1556.   defsubr (&Ssleep_for);
  1557.   defsubr (&Sbaud_rate);
  1558.   defsubr (&Ssend_string_to_terminal);
  1559.  
  1560.   DEFVAR_BOOL ("inverse-video", &inverse_video,
  1561.     "*Non-nil means use inverse-video.");
  1562.   DEFVAR_BOOL ("visible-bell", &visible_bell,
  1563.     "*Non-nil means try to flash the screen to represent a bell.");
  1564.   DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
  1565.     "*Non-nil means no need to redraw entire screen after suspending.\n\
  1566. It is up to you to set this variable to inform Emacs.");
  1567.   DEFVAR_LISP ("window-system", &Vwindow_system,
  1568.     "A symbol naming the window-system under which Emacs is running,\n\
  1569. \(such as `x'), or nil if emacs is running on an ordinary terminal.");
  1570.   DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
  1571.     "Version number of the window system Emacs is running under.");
  1572.   DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
  1573.     "Non-nil means put cursor in minibuffer after any message displayed there.");
  1574.  
  1575.   /* Initialize `window-system', unless init_display already decided it.  */
  1576. #ifdef CANNOT_DUMP
  1577.   if (noninteractive)
  1578. #endif
  1579.     {
  1580.       Vwindow_system_version = Qnil;
  1581.       Vwindow_system = Qnil;
  1582.     }
  1583. }
  1584.